package com.ciwong.epaper.modules.reciteWords.dbreciteword;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ciwong.epaper.common.db.DatabaseHelper;
import com.ciwong.libs.utils.CWLog;

import java.util.ArrayList;
import java.util.List;

public class WordBookStateDao {


    private String TAG="WordBookStateDao";
    private SQLiteDatabase db;

    public WordBookStateDao() {
        this.db = DatabaseHelper.getDatabase();
    }

    public void creatTable(String tableName) {
        CWLog.d(TAG,"creatTable 创建数据库表");
        db.execSQL(DBColumns.getCreateTableWordBookStateString(tableName));
    }

    public void dropTable(String tableName) {
        String sql = "DROP TABLE IF EXISTS " + tableName;
        db.execSQL(sql);
    }

    public boolean checkTableIsExist(String tableName) {
        boolean result = false;
        String sql = "select count(*) as c from Sqlite_master  where type ='table' and name =" +"'"+ tableName.trim()+"';";
        try {
            Cursor cursor = db.rawQuery(sql, null);
            if (cursor.moveToNext()) {
                int count = cursor.getInt(0);
                if (count > 0) {
                    result = true;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 添加数据到数据库对应表中
     *
     * @param tableName
     * @param bookId 书籍的唯一标识
     * @param urlHash 书籍下载链接（变动时说明有更新）
     */
    public boolean add(String tableName, String bookId, int urlHash) {
        ContentValues values = new ContentValues();
        values.put(DBColumns.BOOK_ID, bookId);
        values.put(DBColumns.URL_HASH_CODE, urlHash);
        long result = db.replace(tableName, null, values);
        return result != -1;
    }
    /**
     * 添加数据到数据库对应表中
     *
     * @param tableName
     * @param bookId 书籍的唯一标识
     * @param urlHash 书籍下载链接（变动时说明有更新）
     */
    public boolean update(String tableName, String bookId, int urlHash) {
        ContentValues values = new ContentValues();
        values.put(DBColumns.BOOK_ID, bookId);
        values.put(DBColumns.URL_HASH_CODE, urlHash);
        long result = db.update(tableName,  values,DBColumns.BOOK_ID+"=?",new String[] {bookId});
        return result != -1;
    }

    /**
     * 查询表中所有数据
     *
     * @param tableName
     * @return
     */
    public List<Integer> queryAll(String tableName) {
        List<Integer> list = null;
        Cursor cursor = null;
        try {
            cursor = db.query(tableName, null, null, null, null, null, null);
            list = getWordBookStateList(cursor);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cursor.close();
        }
        return list;
    }

    /**
     * 监测数据库中是否存在对应 id数据
     *
     * @param tableName
     * @param id
     * @return
     */
    public boolean queryIsExistId(String tableName, String id) {

        Cursor cursor = db.query(tableName, null, DBColumns._ID + "=?", new String[]{id}, null, null, null);
        if (cursor.moveToNext()) {
            cursor.close();
            return true;
        }
        cursor.close();
        return false;
    }
    /**
     * 监测数据库中是否存在对应 bookId数据
     *
     * @param tableName
     * @param bookId
     * @return
     */
    public boolean queryIsExistBookid(String tableName, String bookId) {

        Cursor cursor = db.query(tableName, null, DBColumns.BOOK_ID+ "=?", new String[]{bookId}, null, null, null);
        if (cursor.moveToNext()) {
            cursor.close();
            return true;
        }
        cursor.close();
        return false;
    }

    public List<Integer> queryById(String tableName, String id) {
        List<Integer> list = null;
        Cursor cursor = null;
        try {
            cursor = db.query(tableName, null, DBColumns._ID + "=?", new String[]{id}, null, null, null);
            list = getWordBookStateList(cursor);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cursor.close();
        }
        return list;
    }

    public List<Integer> queryByBookId(String tableName, String bookId) {
        List<Integer> list = null;
        Cursor cursor = null;
        try {
            cursor = db.query(tableName, null, DBColumns.BOOK_ID + "=?", new String[]{bookId}, null, null, null);
            list = getWordBookStateList(cursor);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cursor.close();
        }
        return list;
    }


    public boolean delete(String tableName, String bookId) {
        int id = db.delete(tableName, DBColumns.BOOK_ID + "=?", new String[]{bookId});
        return id != -1;
    }

    public boolean deleteAll(String tableName) {
        int id = db.delete(tableName, null, null);
        return id != -1;
    }

    public List<Integer> getWordBookStateList(Cursor cursor) {
        List<Integer> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            int bookUrlHashCode = cursor.getInt(cursor.getColumnIndex(DBColumns.URL_HASH_CODE));
            list.add(bookUrlHashCode);
        }
        return list;
    }

    public int getRowCount(String tableName) {
        return db.query(tableName, null, null, null, null, null, null).getCount();
    }
}
